/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.physics;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.joints.PulleyJoint;
import com.badlogic.gdx.physics.box2d.joints.PulleyJointDef;
import com.jdiv.Const;
import com.jdiv.kernel.JDivSystem;

/** Pulley joint. This requires two ground anchors, two dynamic body anchor points, max lengths for each side, and a pulley ratio. */

public class JointPulley {
	
	PulleyJointDef jointDef;
	Joint joint;
	
	/**
	 * Constructor of JointPulley.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	 
	public JointPulley( Body bodyA, Body bodyB, boolean collideConnected ){
		
	 jointDef = new PulleyJointDef();
	 
	 joint = null;
	 
	 init( bodyA, bodyB, collideConnected );
	}
	
	/**
	 * Initialize the bodies.
	 * @param bodyA Body of object A.
	 * @param bodyB Body of object B.
	 * @param collideConnected Sets if they collide with each other.
	 */
	
   public void init ( Body bodyA, Body bodyB, boolean collideConnected ){
		
		if( joint != null ) return;
		
		jointDef.bodyA = bodyA;
		jointDef.bodyB = bodyB;
		jointDef.collideConnected = collideConnected;
	}
   
   /**
    * Sets the pivot point from object A.
    * @param x Coordinate X.
    * @param y Coordinate Y.
    */
	 
	public void setAnchorA( float x, float y ){
	 jointDef.localAnchorA.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
			                   y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	    * Sets the pivot point from object B.
	    * @param x Coordinate X.
	    * @param y Coordinate Y.
	    */
	 
	public void setAnchorB( float x, float y ){
	 jointDef.localAnchorB.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
			                   y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	    * Set the pivot point of the ground, the object A.
	    * @param x Coordinate X.
	    * @param y Coordinate Y.
	    */
	
	public void setGroundAnchorA( float x, float y ){
		 jointDef.groundAnchorA.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
				                    y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	    * Set the pivot point of the ground, the object B.
	    * @param x Coordinate X.
	    * @param y Coordinate Y.
	    */
		 
	public void setGroundAnchorB( float x, float y ){
		 jointDef.groundAnchorB.set(x * Const.BOX2D_SCREEN_TO_PHYSICS, 
				                    y * Const.BOX2D_SCREEN_TO_PHYSICS);
	}
	
	/**
	 * @param lengthA  The a reference length for the segment attached to bodyA.
	 */
	 
	public void setLengthA( float lengthA ){
	     jointDef.lengthA = lengthA  * Const.BOX2D_SCREEN_TO_PHYSICS;
	}
	
	/**
	 * @param lengthB  The a reference length for the segment attached to bodyB.
	 */
	
	public void setLengthB( float lengthB ){
		 jointDef.lengthB = lengthB  * Const.BOX2D_SCREEN_TO_PHYSICS;
	}
	
	/**
	 * @param ratio The pulley ratio, used to simulate a block-and-tackle.
	 */
	 
	public void setRatio( float ratio ){
	 jointDef.ratio = ratio;
	}
	
	/**
	 * Add the joint to the physical world.
	 * @return The joint in the physical world.
	 */
	 
	public Joint addToWorld(){
	 if( joint != null ) return joint;
	 
	 if( JDivSystem.box2dWorld != null )	
	  joint = JDivSystem.box2dWorld.createJoint(jointDef);
	 
	 return joint;
	}
	
	/**
	 * @return Gets PulleyJoint of LibGdx.
	 */
	
	public PulleyJoint getPulleyJoint(){
		return (PulleyJoint)joint;
	}
	
	/** Delete the joint in the physical world. */
	
	public void delete(){
		if( (JDivSystem.box2dWorld == null) || (joint == null) ) return;
		
		JDivSystem.box2dWorld.destroyJoint(joint);
		joint = null;
	}
}
